<?php

namespace app\article\controller;

use app\article\model\Article;
use app\article\model\Articletype;
use app\article\model\U_a;
use think\cache\driver\Redis;
use think\Controller;
use think\facade\Cache;
use think\facade\Session;
use think\Request;

class Articlelist extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //查询分类信息
        $data = Articletype::all();

        $data1 = Article::all();
        return view('home/index', compact('data', 'data1'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //查询分类信息
        $data = Articletype::all();

        return view('home/create', compact('data'));
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //接收值
        $data = $request->param();

        //添加到数据库
        $res = Article::create($data, true);

        //添加成功
        if ($res) {
            $this->success('新增成功', 'Articlelist/index');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($a_id)
    {
        $redis = new Redis();
        //根据查看详情是阅读统计
        Article::where('a_id', $a_id)->setInc('a_look');

        $res = Article::where('a_id', $a_id)->find();

        //将数据存入缓存
        $redis->set('a_look' . $a_id, $res['a_look']);

        $redis->inc('a_look' . $a_id);
        $a_look = $redis->get('a_look'.$a_id);
        $data = Article::where('a_id', $a_id)->find();

        $previous = Article::order('a_id', 'desc')->where('a_id', '<', $a_id)->limit(1)->find();
        $next = Article::order('a_id', 'asc')->where('a_id', '>', $a_id)->limit(1)->find();

        return view('home/read', compact('data', 'previous', 'next', 'a_look'));
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    //查询分类
    public function type($id)
    {
        //开启redis服务
        $redis = new Redis();

//查询分类信息
        $data = Articletype::all();

        //判断查询出的分类是否在缓存里
        if ($redis->has($id)) {
            $data1 = $redis->get($id);

        } else {
            $data1 = Article::join('articletype', 'article.a_type=articletype.id')->where('a_type', $id)->select();

            $redis->set($id, $data1);

        }
        return view('home/index', compact('data', 'data1'));
    }

    //收藏添加
    public function shou($a_id)
    {
        Session::set('u_id','1');



        if (Session::get('u_id'))
        {
            $data['u_id'] = Session::get('u_id');
            $data['a_id'] = $a_id;
            $res = U_a::create($data,true);
            if ($res)
            {
                $this->success('收藏成功');
            }
        }else{
            $this->error('请先登录');
        }
    }
}
